package com.dhnsoft.代码随想录.数组;

/**
 * ClassName: 长度最小的子数组
 * Package: com.dhnsoft.代码随想录
 * Description:
 *
 * @Author dhn
 * @Create 2024/2/29 10:59
 * @Version 1.0
 */
public class 长度最小的子数组 {

    public static void main(String[] args) {
        int num[] = new int[]{2,3,1,2,4,3};
        int i = minSubArrayLen(7, num);   
        System.out.println(i);
    }

    public static int minSubArrayLen(int target, int[] nums) {
        int sum = 0;
        int result = Integer.MAX_VALUE;
        int j = 0,sub;
        for (int i=0;i<nums.length;i++){
            sum+=nums[i];
            while (sum>=target){
                sub = i-j+1;
                result = Math.min(result,sub);
                sum -=nums[j++];
            }
        }
        return result==Integer.MAX_VALUE?0:result;
    }
}
